package com.pro_one.controller;

import com.pro_one.constant.MessageConstant;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.entity.Result;
import com.pro_one.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    /**
     * 获取用户信息
     *
     * @return
     */
    @RequestMapping("/getUsername")
    public Result getUsername() {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    /**
     * 查询所有用户
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findAllUser")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public PageResult findAllUser(@RequestBody QueryPageBean queryPageBean) {
        PageResult allUser = userService.findAllUser(queryPageBean);
        return allUser;

    }

    /**
     * 添加用户
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("/addUser")
    @PreAuthorize("hasAuthority('USER_ADD')")
    public Result addUser(@RequestBody com.pro_one.pojo.User user,Integer[] roleIds) {
        try {
            userService.addUser(user,roleIds);
            return new Result(true,MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }

    }

    /**
     * 修改用户
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("/updateUser")
    @PreAuthorize("hasAuthority('USER_EDIT')")
    public Result updateUser(@RequestBody com.pro_one.pojo.User user,Integer[] roleIds) {
        try {
            userService.updateUser(user,roleIds);
            return new Result(true,MessageConstant.UPDATE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_USER_FAIL);
        }

    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/deleteUser")
    @PreAuthorize("hasAuthority('USER_DELETE')")
    public Result deleteUser(Integer id) {
        try {
            userService.deleteUser(id);
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }

    }

    /**
     * 根据用户id查询所有与之关联的角色id
     * @param userId
     * @return
     */
    @RequestMapping("/findUserIdAndRoleIdByUserId")
    public Integer[] findUserIdAndRoleIdByUserId(Integer userId) {
             return userService.findUserIdAndRoleIdByUserId(userId);
    }

}
